{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from PIL import Image\n",
    "import tensorflow as tf\n",
    "\n",
    "from utils import img2array, array2img\n",
    "from stn import spatial_transformer_network as transformer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load Images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "DIMS = (600, 600)\n",
    "data_dir = './data/'\n",
    "\n",
    "# load 4 cat images\n",
    "img1 = img2array(data_dir + 'cat1.jpg', DIMS, expand=True)#, view=True)\n",
    "img2 = img2array(data_dir + 'cat2.jpg', DIMS, expand=True)\n",
    "img3 = img2array(data_dir + 'cat3.jpg', DIMS, expand=True)\n",
    "img4 = img2array(data_dir + 'cat4.jpg', DIMS, expand=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input Img Shape: (4, 600, 600, 3)\n"
     ]
    }
   ],
   "source": [
    "input_img = np.concatenate([img1, img2, img3, img4], axis=0)\n",
    "B, H, W, C = input_img.shape\n",
    "print(\"Input Img Shape: {}\".format(input_img.shape))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Identity Transform\n",
    "\n",
    "We'll be using an identity transform as a sanity check. This means the output image should look like the input image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# identity transform\n",
    "theta = np.array([[1., 0, 0], [0, 1., 0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = tf.placeholder(tf.float32, [None, H, W, C])\n",
    "\n",
    "with tf.variable_scope('spatial_transformer'):\n",
    "    theta = theta.astype('float32')\n",
    "    theta = theta.flatten()\n",
    "\n",
    "    # define loc net weight and bias\n",
    "    loc_in = H*W*C\n",
    "    loc_out = 6\n",
    "    W_loc = tf.Variable(tf.zeros([loc_in, loc_out]), name='W_loc')\n",
    "    b_loc = tf.Variable(initial_value=theta, name='b_loc')\n",
    "    \n",
    "    # tie everything together\n",
    "    fc_loc = tf.matmul(tf.zeros([B, loc_in]), W_loc) + b_loc\n",
    "    h_trans = transformer(x, fc_loc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "y: (4, 600, 600, 3)\n"
     ]
    }
   ],
   "source": [
    "# run session\n",
    "sess = tf.Session()\n",
    "sess.run(tf.global_variables_initializer())\n",
    "y = sess.run(h_trans, feed_dict={x: input_img})\n",
    "print(\"y: {}\".format(y.shape))\n",
    "array2img(y[0]).show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rotation\n",
    "\n",
    "Let's try rotating the picture by 45 degrees."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils import deg2rad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# initialize affine transform tensor `theta`\n",
    "degree = 45\n",
    "theta = np.array([\n",
    "    [np.cos(deg2rad(degree)), -np.sin(deg2rad(degree)), 0], \n",
    "    [np.sin(deg2rad(degree)), np.cos(deg2rad(degree)), 0]\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = tf.placeholder(tf.float32, [None, H, W, C])\n",
    "\n",
    "with tf.variable_scope('spatial_transformer'):\n",
    "    theta = theta.astype('float32')\n",
    "    theta = theta.flatten()\n",
    "\n",
    "    # define loc net weight and bias\n",
    "    loc_in = H*W*C\n",
    "    loc_out = 6\n",
    "    W_loc = tf.Variable(tf.zeros([loc_in, loc_out]), name='W_loc')\n",
    "    b_loc = tf.Variable(initial_value=theta, name='b_loc')\n",
    "    \n",
    "    # tie everything together\n",
    "    fc_loc = tf.matmul(tf.zeros([B, loc_in]), W_loc) + b_loc\n",
    "    h_trans = transformer(x, fc_loc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "y: (4, 600, 600, 3)\n"
     ]
    }
   ],
   "source": [
    "# run session\n",
    "sess = tf.Session()\n",
    "sess.run(tf.global_variables_initializer())\n",
    "y = sess.run(h_trans, feed_dict={x: input_img})\n",
    "print(\"y: {}\".format(y.shape))\n",
    "array2img(y[0]).show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
